﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MySqlManagerData : System.Web.UI.Page
{
    private int perPage = 2;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            LoadData(0*perPage,perPage);
            LBtnNext.PostBackUrl = "?page=2";
            if (Request.QueryString["action"]!=null) {
                long id = Convert.ToInt64(Request.QueryString["id"]);
                switch (Request.QueryString["action"]) {
                    case "edit":
                        UpdateInfo(id);
                        break;
                    case "delete":
                        DeleteInfo(id);
                        break;
                }
            }
        }
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int page = Convert.ToInt32(Request.QueryString["page"]);
        LoadData((page - 1) * perPage, perPage);
        LBtnNext.PostBackUrl = "?page=" + (page + 1);
    }

    private void LoadData(int start, int num)
    {

        try
        {
            Literal1.Text = "<table>\n";
            Literal1.Text += "<tr>\n";
            Literal1.Text += "<th>序号</th>\n";
            Literal1.Text += "<th>标题</th>\n";
            Literal1.Text += "<th>内容</th>\n";
            Literal1.Text += "<th>发布时间</th>\n";
            Literal1.Text += "<th>作者</th>\n";
            Literal1.Text += "<th>查看次数</th>\n";
            Literal1.Text += "<th>分类</th>\n";

            Literal1.Text += "</tr>\n";
            foreach (var data in InfoDao.FindAll(start, num))
            {
                Literal1.Text += "<tr>\n";
                Literal1.Text += "<td>" + data.Id + "</td>\n";
                Literal1.Text += "<td>" + data.Title + "</td>\n";
                Literal1.Text += "<td>" + data.Content + "</td>\n";
                Literal1.Text += "<td>" + data.PublishTime + "</td>\n";
                Literal1.Text += "<td>" + data.Author + "</td>\n";
                Literal1.Text += "<td>" + data.ClickNumber + "</td>\n";
                Literal1.Text += "<td>" + data.CategoryMark + "</td>\n";
                Literal1.Text += "<td>";
                Literal1.Text += "<a href=\"?action=edit&id="+data.Id+"\">修改";
                Literal1.Text += "<a href=\"?action=delete&id=" + data.Id + "\">删除";
                Literal1.Text += "</td>";
                Literal1.Text += "</tr>\n";
            }
            Literal1.Text += "</table>\n";
        }
        catch (Exception)
        {
            Literal1.Text = "数据加载异常！";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try {
            if (HfId.Value.Equals("-1"))    //新增界面
            {
                AddInfo();
            }
            else {  //修改记录
                SaveInfo();
            }
        } catch {
            Literal1.Text = "数据处理异常！";
        }
    }

    private void AddInfo()
    {
        Info data = new Info();
        data.Title = TbTitle.Text.Trim();
        data.Content = TbContent.Text.Trim();
        data.Author = TbAuthor.Text.Trim();
        data.PublishTime = DateTime.Now;
        data.ClickNumber = 300;
        data.CategoryMark = "xyyw"; //实际应该从下拉列表控件获取数据
        long insertId = InfoDao.Insert(data);
        Debug.WriteLine("插入数据返回ID:"+insertId);
    }

    private void DeleteInfo(long id)
    {
        InfoDao.Delete(id);
    }

    private void UpdateInfo(long id)
    {
        Info data = InfoDao.Find(id);
        TbTitle.Text = data.Title;
        TbContent.Text = data.Content;
        TbAuthor.Text = data.Author;
        HfId.Value = data.Id.ToString();
    }

    private void SaveInfo()
    {
        Info data = InfoDao.Find(Convert.ToInt64(HfId.Value));
        data.Title = TbTitle.Text.Trim();
        data.Content = TbContent.Text.Trim();
        data.Author = TbAuthor.Text.Trim();
        InfoDao.Update(data);
    }

}